package com.xu.ai.model.openai.controller;

import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.content.Media;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import reactor.core.publisher.Flux;

import java.net.URI;

/**
 * 多模态
 *
 * @author xuguan
 * @since 2025/10/9
 */
@RestController
@RequestMapping("/api/multimodality")
public class MultimodalityController {
    private final OpenAiChatModel openAiChatModel;

    public MultimodalityController(OpenAiChatModel openAiChatModel,
                                   OpenAiImageModel openAiImageModel) {
        this.openAiChatModel = openAiChatModel.mutate()
                .defaultOptions(OpenAiChatOptions.builder().model("qwen3-omni-flash").temperature(0.8).build())
                .build();
    }

    @GetMapping(value = "/image-to-text", produces = "text/html;charset=UTF-8")
    public Flux<String> image2Text() {
        final URI uri = URI.create("https://java2ai.com/img/user/ai/overview/1.0.0/spring-ai-alibaba-architecture.png");
        var userMessage = UserMessage.builder()
                .text("请用中文描述这张图片你看到了什么?") // content
                .media(new Media(MimeTypeUtils.IMAGE_PNG, uri)) // media
                .build();
        return this.openAiChatModel.stream(userMessage);
    }

}
